<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
 */
class storechannel_db extends Model
{
	public $pk = 'channel_id';
	public $table = 'ptx_store_channel';

	var $select_fields = " ptx_store_channel.* ";

	public function search($page,$pagesize){
		$sort = " ptx_store_channel.display_order DESC ";
		return $this->pager($page, $pagesize)->findAllJoin('',$sort,' ptx_store_channel.* ');
	}
	
	public function getAllChannels(){
		$rs = pcache('r', 'store_category');
		if(!$rs){
			return $this->updateAllChannelCache();
		}
		return $rs;
	}

	public function updateAllChannelCache(){
		if($rs = $this->findAll(null,' ptx_store_channel.display_order DESC ')){
			pcache('w', 'store_category',$rs);
			return $rs;
		}
		return FALSE;
	}

	public function find_one($conditions=NULL){
		$fields = $this->select_fields;
		return $this->findJoin($conditions,null,$fields);
	}

	public function deleteChannel($conditions){
		$channel_id = $this->escape($conditions['channel_id']);
		if($channel_id){
			load_db('share')->change_channel($conditions['channel_id'],1);
			$this->delete($conditions);
			$this->updateAllChannelCache();
		}
	}
	
	public function updateChannelByid($catid,$data){
		$this->update(array('channel_id'=>$catid), $data);
		$this->updateAllChannelCache();
	}
	
	public function add_one($data){
		$this->create($data);
		$this->updateAllChannelCache();
	}

	public function getChannelByid($cid){
		$categories = $this->getAllChannels();
		foreach ($categories as $category) {
			if($category['channel_id']==$cid){
				return $category;
			}
		}
	}

}
